
//
//  MoviesViewController.m
//  WD-Movies
//
//  Created by 贺泌钦 on 2019/1/23.
//  Copyright © 2019 chuck. All rights reserved.
//

#import "MoviesViewController.h"
#import "CollectionViewCell.h"
#import "DCCycleScrollView.h"
#import "MoviesModel.h"

@interface MoviesViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout,DCCycleScrollViewDelegate>
@property(nonatomic , strong)UIScrollView *scr;
@property(nonatomic,strong)UICollectionView *clv;
@property(nonatomic , strong)NSMutableArray *datasource;

@end

@implementation MoviesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.datasource = [NSMutableArray new];
    self.navigationController.navigationBar.hidden = YES;
    [self setUI];
    [self loadData];
    
//    添加网格
    [self.view addSubview:self.clv];
}
-(void)setUI{
    
    NSArray *imageArr = @[@"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          @"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          @"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          @"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          ];
    DCCycleScrollView *banner = [DCCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 30, self.view.frame.size.width, 250) shouldInfiniteLoop:YES imageGroups:imageArr];
    //    banner.placeholderImage = [UIImage imageNamed:@"placeholderImage"];
    //    banner.cellPlaceholderImage = [UIImage imageNamed:@"placeholderImage"];
    banner.autoScrollTimeInterval = 2;
    banner.autoScroll = YES;
    banner.isZoom = YES;
    banner.itemSpace = 0;
    banner.imgCornerRadius = 10;
    banner.itemWidth = self.view.frame.size.width - 200;
    banner.delegate = self;
    
     [self.view addSubview:banner];
}
//点击图片的代理
-(void)cycleScrollView:(DCCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"index = %ld",(long)index);
}
//网格
- (UICollectionView *)clv{
    if (!_clv) {
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(126, 164);
        _clv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 300, WIDTH, HEIGHT-250) collectionViewLayout:layout];
        _clv.backgroundColor =[UIColor whiteColor];
        _clv.delegate = self;
        _clv.dataSource = self;
        [_clv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
    }
    return _clv;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    
    UIScrollView *scr= [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    scr.contentSize = CGSizeMake(5*150, 0);
    
    [self.view addSubview:scr];
    
    
    
    return cell;
}

//http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userId=18&sessionId=15320748258726&page=1&count=10
-(void)loadData{
//    NSDictionary *pdic = @{@"userId":@"18",@"sessionId":@"15320748258726",@"page":@"1",@"count":@"10"};
    
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    [manger GET:@"http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userId=18&sessionId=15320748258726&page=1&count=10" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"//////%@",responseObject[@"result"]);
        for (NSDictionary *dic in responseObject[@"result"]) {
            MoviesModel *model = [[MoviesModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.datasource addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.clv reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


@end
